<template>
  <div class="app-container">
    <h1 ref="myh1">App 根组件</h1>
    <button @click="showThis">打印this</button>
    <button @click="resetLeft">重置Left组件count的值为0</button>
    <hr/>
    <input type="text" v-if="inputVisible" @blur="showButton" ref="iptRef"/>
    <button v-else @click="showInput">展示输入框</button>
    <hr/>
    <div class="box">
      <!-- 渲染 Left 组件和 Right 组件 -->
      <Left ref="comLeft"></Left>
    </div>
  </div>
</template>

<script>

import Left from '@/components/Left.vue'
export default {
  data() {
    return {
      //控制输入框和按钮的按需切换
      //默认值为false，表示默认展示按钮，隐藏输入框
      inputVisible: false
    }
  },
  methods: {
    showThis() {
      //当前App组件的实例对象
      console.log(this)
      // console.log(this.$refs.myh1)
      this.$refs.myh1.style.color = 'red'
    },
    //点击按钮，重置Left组件的count值
    resetLeft() {
      // this.$refs.comLeft.count = 0
      this.$refs.comLeft.resetCount()
    },
    showInput() {
      //1.切换布尔值，展示文本框
      this.inputVisible = true
      //2.文本框自动聚焦
      this.$nextTick(() => {
        this.$refs.iptRef.focus()
      })
    },
    showButton() {
      this.inputVisible = false
    }
  },
  //私有子组件
  components: {
    Left
  }
}
</script>

<style lang="less">
.app-container {
  padding: 1px 20px 20px;
  background-color: #efefef;
}
.box {
  display: flex;
}
</style>
